/**
 * Copyright (c) 2018-2023, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: condition test 
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2023-08-25     JasonHu           Init
 */

#include <base/condition.h>
#include <test/utest.h>

#ifdef CONFIG_NX_UTEST_SCHED_CONDITION

NX_TEST(NX_ConditionInit)
{
    NX_Condition cond;
    NX_EXPECT_NE(NX_ConditionInit(NX_NULL, 0), NX_EOK);
    NX_EXPECT_EQ(NX_ConditionInit(&cond, 0), NX_EOK);
}

NX_TEST(NX_ConditionCreate)
{
    NX_Condition *cond;

    cond = NX_ConditionCreate(0);
    NX_EXPECT_NE(cond, NX_NULL);
    NX_EXPECT_EQ(NX_ConditionDestroy(cond), NX_EOK);
}

NX_TEST(NX_ConditionWaitTimeout)
{
    NX_Condition cond;
    NX_Mutex mutex;

    NX_EXPECT_EQ(NX_ConditionInit(&cond, 0), NX_EOK);
    NX_EXPECT_EQ(NX_MutexInit(&mutex), NX_EOK);
    
    NX_EXPECT_NE(NX_ConditionWaitTimeout(NX_NULL, &mutex, 1000), NX_EOK);
    NX_EXPECT_NE(NX_ConditionWaitTimeout(NX_NULL, NX_NULL, 1000), NX_EOK);

    NX_EXPECT_NE(NX_ConditionWaitTimeout(&cond, &mutex, 1000), NX_EOK);
    NX_EXPECT_NE(NX_ConditionWaitTimeout(&cond, &mutex, 0), NX_EOK);

    NX_EXPECT_EQ(NX_MutexLock(&mutex), NX_EOK);
    NX_EXPECT_NE(NX_ConditionWaitTimeout(&cond, &mutex, 1000), NX_EOK);
    NX_EXPECT_EQ(NX_MutexUnlock(&mutex), NX_EOK);
}

NX_TEST(NX_ConditionSignal)
{
    NX_Condition cond;

    NX_EXPECT_EQ(NX_ConditionInit(&cond, 0), NX_EOK);
    NX_EXPECT_NE(NX_ConditionSignal(NX_NULL), NX_EOK);
    NX_EXPECT_EQ(NX_ConditionSignal(&cond), NX_ENORES);
}

NX_TEST(NX_ConditionBroadcast)
{
    NX_Condition cond;

    NX_EXPECT_EQ(NX_ConditionInit(&cond, 0), NX_EOK);
    NX_EXPECT_NE(NX_ConditionBroadcast(NX_NULL), NX_EOK);
    NX_EXPECT_EQ(NX_ConditionBroadcast(&cond), NX_ENORES);
}

NX_TEST_TABLE(NX_Condition)
{
    NX_TEST_UNIT(NX_ConditionInit),
    NX_TEST_UNIT(NX_ConditionCreate),
    NX_TEST_UNIT(NX_ConditionWaitTimeout),
    NX_TEST_UNIT(NX_ConditionSignal),
    NX_TEST_UNIT(NX_ConditionBroadcast),
};

NX_TEST_CASE(NX_Condition);

#endif
